<?
require("../../common/init.php");
require("author.php");
require("upfile.php");
require(ROOT."common/output_csv.php");

$gee=$coupon; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
//$cate=$product_category; // cate = category, 分类

$title=COUPON;
$url='coupon';

// varible 变量
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"]; // current page 当前页码
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);
$export=0; if (isset($_GET["export"])) $export = clean($_GET["export"]);
$cateid=0;

// 明天 tomorrow
$tomorrow = date("Y-m-d H:i:s", time());

// sql action / sql 动作
switch ($action){
	case "add": sql_insert(); break;
	case "deletes": sql_delete(); break;
}

if($export==0){ // 显示在页面 show in page
	$gee->rows=15;
	$where=array();
	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where);
}
else { // 导出到CSV, export to CSV
	$where=array();
	$head=array('#','卡号','类型','面额','状态','用户');
	$info=$gee->get_rows_advanced(102400,$where,'id','desc',1);
	$data=array();
	$arr_cate=$code_array['discount_type'];
	foreach($info as $val){
		$status=$val['is_used']?'已使用':'未使用';
		$tem_type=$arr_cate[$row['discount_type']]['name_'.$lang];
		$customer_name=' ';
		$data[]=array(
			$val['id'],$val['code'],$tem_type,$val['discount_value'],$status,$customer_name
		);
	}
	output_csv($head,$data);
	// while($row=$info->fetch(PDO::FETCH_BOTH) ){
	// 	print_r($row);
	// }
	exit;
}

function add()
{
	global $gee, $url, $h, $id;
	$tem=array();
	echo $h->form($url.'.php', 0, '');
  	show_box($tem,true, true);
	echo $h->hidden('action', 'add');
	echo $h->former();
}
function delete($id)
{
	global $gee,$url;
	if($gee->delete($id)) wheel($url.'.php');
}	
function show_info()
{
	global $gee,$info,$id;
	if($id==0) show_boxes($info);
	else show_box($info, false, false);
}
function sql_insert()
{
	global $gee,$url;
	$qty=$_POST["qty"];
	$discount_type=$_POST["discount_type"];
	$discount_value=$_POST["discount_value"];
	$tem=$gee->get_last();
	$num_begin=80000000+$tem['id'];
	$ok=0;
	$str='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	for($i=1;$i<$qty+1;$i++){
		$tem_num=$num_begin+$i;
		$tem_rand=rand(0,25);
		$tem_pre=substr($str,$tem_rand,1);
		$tem_code=$tem_pre.strtoupper(substr(sha1($tem_num.'ecomgear'),0,14));
		$arr=array(
			'code'=>$tem_code,
			'discount_type'=>$discount_type,
			'discount_value'=>$discount_value,
			'expire_date'=>$_POST['expire_date']
		);
		if($gee->insert($arr)) $ok=1;
	}
	if($ok)echo '<script type="text/javascript">alert("添加成功! Success!");location="'.$url.'.php";</script>';
}
function sql_delete()
{
	global $gee,$page,$url,$cateid;
	$page=iso(@$_POST["page"]); $cateid=iso(@$_POST["cateid"]);
	$all=$_POST["all"];
	$arrdelete=array();
	if(count($all)>0)foreach($all as $a){ $arrdelete[]=$a; }
	if($gee->delete_rows($arrdelete))
	wheel($url.'-'.$cateid.'-p'.$page.'.html');
}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end sales_order.php